package com.itump.base.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 标题: JAVA标准化管理子系统<br>
 * 作者: itump<br>
 * 功能描述: 实现验证码类<br>
 
 
 */
public class CheckCodeServlet extends HttpServlet {
	private static final long serialVersionUID = -479001550220626218L;
	private static int WIDTH=60;
	private static int HEIGHT=20;
	/**
	 * Constructor of the object.
	 */
	public CheckCodeServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		 this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 
		  
		        HttpSession session = request.getSession();    
		        response.setContentType("image/jpeg");
		        ServletOutputStream sos = response.getOutputStream();
		        
		        response.setHeader("Pragma", "No-cache");
		        response.setHeader("Cache-Control", "no-cache");
		        response.setDateHeader("Expires", 0);
		        
		        BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
		        Graphics g=image.getGraphics();
		        
		        char[] rands = generateCheckCode();
		        drawBackground(g);
		        drawRands(g,rands);
		        g.dispose();
		        
		        ByteArrayOutputStream bos= new ByteArrayOutputStream();
		        ImageIO.write(image,"JPEG",bos);
		        byte[] buf = bos.toByteArray();
		        response.setContentLength(buf.length);
		        sos.write(buf);
		        bos.close();
		        sos.close();
		        session.setAttribute("check_code", new String(rands));
                
	}
	 private void drawBackground(Graphics g) {
	        g.setColor(new Color(0xDCDCDC));
	        g.fillRect(0, 0, WIDTH, HEIGHT);
	        Random random = new Random();
	        for(int i=0;i<120;i++){
	            int x = random.nextInt(WIDTH);
	            int y = random.nextInt(HEIGHT);
	            int red = random.nextInt(255);
	            int green = random.nextInt(255);
	            int blue = random.nextInt(255);
	            g.setColor(new Color(red,green,blue));
	            g.drawOval(x, y, 1, 0);
	        }
	    }
	    private void drawRands(Graphics g, char[] rands){
	        g.setColor(Color.BLACK);
	        g.setFont(new Font(null,Font.ITALIC|Font.BOLD,18));
	        g.drawString(""+rands[0], 1, 17);
	        g.drawString(""+rands[1], 16, 15);
	        g.drawString(""+rands[2], 31, 18);
	        g.drawString(""+rands[3], 46, 16);
	    }
	    private char[] generateCheckCode() {
	       // String chars = "0123456789abcdeFGhIJkLmnOPQRSTuvwxyz";
	        String chars = "0123456789";
	        char[] rands = new char[4];
	        Random random = new Random();
	        for(int i=0;i<4;i++){
	            int rand = random.nextInt(10);
	            rands[i]=chars.charAt(rand);
	        }
	        return rands;
	 }

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
